define([
    'app',
    'jquery',
    'angular',
    'services/EmotionService',
    'controllers/common/ImgViewController'
], function (app, $, angular) {

    'use strict';

    var deps = ['$timeout', '$modal', 'EmotionService'];

    function directive($timeout, $modal, EmotionService) {
        return {
            link: function ($scope, element, attrs) {
                var filters = [];

                $scope.$watch(attrs.fcBindHtml, function (newValue) {
                    var html = angular.copy(newValue);

                    var parseConfig = {
                        abbreviate: attrs.abbreviate === 'true',
                        imgViewable: attrs.imgviewable === 'true',
                        emotionType: attrs.emotionType
                    };

                    element.html(getAsTrustedHtml(html, parseConfig));

                    /** 判断是否需要有点击图片展开大图的功能 **/
                    if (!parseConfig.imgViewable) {
                        return;
                    }

                    $timeout(function () {
                        var $img = element.find('.imgViewAble');
                        $img.off('click');
                        $img.on('click', function (event) {
                            var picId = $(this).attr('picId');
                            event.stopPropagation();
                            if (!picId || picId === '') {
                                return;
                            }
                            $modal.open({
                                templateUrl: 'views/common/imgView/ImgView.html',
                                controller: 'ImgViewController',
                                backdrop: true,
                                resolve: {
                                    picId: function () {
                                        return picId;
                                    }
                                },
                                keyboard: true,
                                windowClass: 'imgView-dialog-window fancy-scrollbar'
                            });
                        });
                    }, 50);
                });

                function getAsTrustedHtml(html, parseConfig) {
                    if (!html) {
                        return html;
                    }

                    return filter(html, parseConfig);
                }

                function filter(html, parseConfig) {
                    angular.forEach(filters, function (item) {
                        html = item(html, parseConfig);
                    });

                    return html;
                }

                function emotionsFilter(html, parseConfig) {
                    return EmotionService.resolve(html, parseConfig);
                }

                function imagesFilter(html, parseConfig) {
                    //处理图片 ，如[5398159361386b469752a474]
                    var reg = /\[([a-z0-9]{24})\]/g;

                    if (parseConfig.abbreviate) {
                        return html.replace(reg, '[图片]');
                    }
                    return html.replace(reg, '<img src="restful/fileUploadController/showPic/$1?picType=2" class="imgViewAble hand" picid="$1">');
                }

                function lineFilter(html, parseConfig) {
                    //处理换行符
                    var reg = /(\r\n)|(\n)|(\r)|(<br\/?>)/g;
                    if (parseConfig.abbreviate) {
                        return html.replace(reg, ' ');
                    }
                    return html.replace(reg, '<br>');
                }

                function plainTextFilter(html, parseConfig) {
                    if (parseConfig.abbreviate) {
                        return $('<div></div>').append(html).text();
                    }
                    return html;
                }

                filters.push(imagesFilter);
                filters.push(lineFilter);
                filters.push(plainTextFilter);
                filters.push(emotionsFilter);
            }
        };
    }

    directive.$inject = deps;
    app.lazy.directive('fcBindHtml', directive);

});